package script.groovy

import cn.hutool.core.lang.Console
import cn.hutool.db.Db
import com.data.boot.model.Field
import com.data.boot.model.Record


/**
 * Created by dwk on 2019/6/5.
 */


def load(String sql, String path, Record record) {
    List<String[]> rowList = new ArrayList<>()
    new File(path).eachLine {
        line ->
            String[] row = parseLine(line, record)
            rowList.add(row)
            int batch = 10000 //批量值
            if (rowList.size() % batch == 0 && rowList.size() > 0) {//批量插入
                String[][] rows = new String[batch][record.getLen()]
                rowList.toArray(rows)
                Db.use().executeBatch(sql, rows)
                rowList.clear()
            }
    }
    if (rowList.size() > 0) {//插入剩余
        String[][] rows = new String[rowList.size()][record.getLen()]
        rowList.toArray(rows)
        Db.use().executeBatch(sql, rows)
    }

}


def parseLine(String line, Record record) {
    def fields = record.getFields()
    List<String> columns = new ArrayList<>()
    for (int i = 0; i < fields.size(); i++) {
        String column = line[0..line.indexOf(fields.get(i).getTerminator()) +
                fields.get(i).getTerminator().length() - 1].replace(fields.get(i).getTerminator(), '')
        if (fields.get(i).getFlag()) {
            columns.add(column)
        }
        if (fields.get(i).getTerminator() != '\n') {
            line = line[column.length() + fields.get(i).getTerminator().length()..-1]
        }
    }
    String[] row = new String[columns.size()]
    columns.toArray(row)
}
//String[] terminators = ['\\', '\\ ', ' ', ' ', ' ', ' [', ']  "', '" ', '  ', '  \\', '\\ \\', '\\ ', ' ', '']
List<Field> list = new ArrayList<>()
Record record = new Record()
list.add(new Field('\\', false))
list.add(new Field('\\', true))
list.add(new Field(' ', false))
list.add(new Field(' ', false))
list.add(new Field(' ', false))
list.add(new Field(' [', false))
list.add(new Field(']  "', true))
list.add(new Field('" ', true))
list.add(new Field('  ', false))
list.add(new Field('  \\', false))
list.add(new Field('\\ \\', true))
list.add(new Field('\\ ', true))
list.add(new Field(' ', false))
list.add(new Field('\n', true))
record.setFields(list)
String sql = 'INSERT INTO od_infoplus_access_log VALUES (?, ?, ?, ?, ?, ?)'
String path = 'C:\\Users\\Administrator\\Desktop\\localhost_access_log.2019-05-28.txt'
long startTime = System.currentTimeMillis() //获取开始时间
Db.use().execute('TRUNCATE TABLE od_infoplus_access_log')
load(sql, path, record)
long endTime = System.currentTimeMillis() //获取结束时间
Console.log("程序运行时间：" + (endTime - startTime) / 1000 + "s"); //输出程序运行时间
